<?php
/***
 * 描述：客户地址管理
 * @category		Mobile		
 * @package			Action			
 * @subpackage		
 * @author			wanght <whtaok@gmail.com>
 * @since			2013-4-6
 * @version			$Id: CustAddressAction.class.php 12 2013-10-22 08:31:42Z whtaok@gmail.com $
 */
class CustAddressAction extends AuthWwwAction {
	/**
	 * 描述：地址列表
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function indexAct() {
		$this->setPageTitle('配送地址管理');
		$mod = new CustAddressModel();
		$modWhere = array(
				'cust_id'=>session(SES_CUST_ID),
			);
		$list = $mod->setModWhere($modWhere)->order('is_default desc')->select();
		$this->assign('list',$list);
		$this->assign('cust',session(SES_CUST));
		$this->display('Cust/address');
	}
	
	/**
	 * 描述：添加地址
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function doAddAct() {
		$data = array(
				'cust_id'=>session(SES_CUST_ID),
				'name'=> I('name'),
				'mobile'=>I('mobile'),
				'address'=>I('address'),
				'is_default'=>I('is_default',0,''),
		);
		$mod = new CustAddressModel();
		if($mod->create($data)){
			$lastId = $mod->add();
			if($lastId){
				//如果设置为默认地址，则更新其他地址为非默认
				if($data['is_default']=='1'){
					$modWhere['id'] = array('neq',$lastId);
					$modWhere['cust_id']=session(SES_CUST_ID);
					$mod->where($modWhere)->setField('is_default',0);
				}
				$rtnData = array(
					'id'=>$lastId,
					'error'=>0,
					'message'=>'保存成功',
				);
				
			}else{
				$rtnData = array(
					'error'=>1,
					'message'=>'保存失败',
				);
			}
		}else{
			$rtnData = array(
				'error'=>1,
				'message'=>$mod->getError(),
			);
		}
		$this->ajaxReturn($rtnData);
	}
	
	/**
	 * 描述：更新地址
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function doUpdAct() {
		
		$data = array(
				'name'=> I('name'),
				'mobile'=>I('mobile'),
				'address'=>I('address'),
				'is_default'=>I('is_default',0,''),
		);

		$mod = new CustAddressModel();
		$modWhere = array(
			'id'=>I('id',0,''),
			'cust_id'=>session(SES_CUST_ID),
		);
		if($mod->create($data)){
			$flag = $mod->setModWhere($modWhere)->save();
			if($flag!==false){
				$rtnData = array(
						'error'=>0,
						'message'=>'修改成功',
				);
				//如果设置为默认地址，则更新其他地址为非默认
				if($data['is_default']=='1'){
					$modWhere['id'] = array('neq',$modWhere['id']);
					$mod->where($modWhere)->setField('is_default',0);
				}
				
			}else{
				$rtnData = array(
						'error'=>1,
						'message'=>'修改失败',
				);
			}
		}else{
			$rtnData = array(
					'error'=>1,
					'message'=>$mod->getError(),
			);
		}
		$this->ajaxReturn($rtnData);
	}
	
	/**
	 * 描述：删除地址
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function doDelAct() {
		$mod = new CustAddressModel();
		$modWhere = array(
				'id'=>I('id'),
				'cust_id'=>session(SES_CUST_ID),
		);
		$flag = $mod->where($modWhere)->delete();
		if($flag!==false){
			$rtnData = array(
						'error'=>0,
						'message'=>'删除成功',
				);
		}else{
			$rtnData = array(
					'error'=>1,
					'message'=>'删除失败',
			);
		}
		$this->ajaxReturn($rtnData);
	}
}

?>